package com.WaVel.controller.admin;

import com.Emplyment.context.BaseContext;
import com.Emplyment.dto.ReUserPasswordDto;
import com.Emplyment.dto.UserRegisterDto;
import com.Emplyment.entity.UserMessage;
import com.Emplyment.result.Result;
import com.WaVel.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/userInfo")
    public Result<UserMessage> userMessageResult(){

        Long id = BaseContext.getCurrentId();
        log.info("获取用户信息，id为：{}",id);
        UserMessage userMessage = userService.userMessageResult(id);
        return Result.success(userMessage);

    }

    @PutMapping("/update")
    public Result updateUserMesssage(@RequestBody UserMessage dto){

        log.info("修改用户信息为：{}",dto);
        userService.updateUser(dto);
        Result.success().setMsg("操作成功");
        return Result.success();

    }

    @PatchMapping("/updateAvatar")
    public Result updateAvatar(@RequestParam String avatarUrl){

        Long id = BaseContext.getCurrentId();
        log.info("修改后的图片地址为:{},id为：{}",avatarUrl,id);
        userService.updateAvatar(avatarUrl,id);
        return Result.success();
    }

    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody ReUserPasswordDto dto){
        log.info("修改密码，旧密码：{},新密码：{},确认密码：{}",dto.getOldPassword(),dto.getNewPassword(),dto.getConfirmPassword());
        if(!dto.getNewPassword().equals(dto.getConfirmPassword())){
            return Result.error("两次密码输入不一致");
        }
        userService.updatePassword(dto);
        return Result.success();
    }


}
